home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9951 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question about visibility of local variables
  5. Date: 5 Mar 1996 20:30:02 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4hh1iq$pvr@werple.net.au>
  8. References: <4hfkpc$9em@guardian.forbin.com>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. genesis@forbin.com (Dances with Demons) writes:
  12.  
  13. >I am in the process of learning C++ which surprised me in being 
  14. >as awesome as everyone kept telling me.  It is literally better
  15. >than anything I ever expected.  I ran into one dark area for me
  16. >in the area of automatic variables. 
  17. >   It said in the book I'm studying, "Object-Oriented Programming
  18. >in C++, by Robert Lafore", that when a function is executed, the
  19. >and the variable is defined, it will continue to exist until the
  20. >function is exited.  When this happens, the memory that the variable
  21. >is freed up and is used by other variables/function calls, etc.
  22.  
  23. >The question is:  When you define a variable in main(), and call another 
  24. >function, or even when defining a local variable in a function that calls 
  25. >another function, why are the local variables not destroyed.
  26. >   I had a thought that maybe since main() called the function, it still had 
  27. >control, (at least as far as variable usage went) and therefore didn't
  28. >free up the memory until the function is actually completed, (End
  29. >of the program), but I wasn't sure.  Could someone please verify that this is 
  30. >indeed the case, or refute it and give me the real reason.
  31.  
  32. Automatic variables are created on the program's stack. A stack is a
  33. last-on, first-off form of data storage, like a stack of trays at some
  34. take-away food restaurants. When a function is entered, local variables
  35. are "pushed" onto the stack and "popped" off when the function exits. If a
  36. function calls another function, any local variables in the called
  37. function are pushed onto the stack also, on top of the ones the calling
  38. function created for itself. Therefore, the stack keeps growing higher as
  39. more functions within functions are entered. As each function returns, its
  40. own variables are popped off, leaving the stack in the state it was before
  41. the function was called, i.e., with the calling function's variables on
  42. top once again. So nothing is destroyed when another function is called.
  43. This means that local variables in 'main' survive for the entire duration
  44. of the program (barring disaster, or a call to exit()).  Parameters passed
  45. to functions are also stored on the stack. 
  46.  
  47. David White
  48. davidw@werple.mira.net.au
  49.